//
//  ST_GiftRankCell.swift
//  btvideo
//
//  Created by mac on 2020/7/2.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON

class ST_GiftRankCell: UITableViewCell {
    
    static let height:CGFloat = 48+20
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
    
        self.contentView.backgroundColor = UIColor.white

        contentView.addSubview(left_number_label)
        left_number_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
            make.height.equalTo(24)
            make.width.greaterThanOrEqualTo(24)
        }
        contentView.addSubview(left_image_view)
        left_image_view.snp.makeConstraints { (make) in
            make.edges.equalTo(left_number_label)
        }
        contentView.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(45)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(48)
        }
        
        contentView.addSubview(user_name_label)
        contentView.addSubview(sweet_image_view)
        user_name_label.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(avatar_image_view.snp.right).offset(10)
            make.width.lessThanOrEqualTo(ScreenWidth-107-100)
        }
        
        sweet_image_view.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(34)
        }
        
        contentView.addSubview(sign_image_container)
        sign_image_container.snp.makeConstraints { (make) in
            make.left.equalTo(user_name_label.snp.right).offset(4)
            make.centerY.equalTo(user_name_label).offset(-1)
            make.height.equalTo(18)
            make.width.equalTo(70)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    fileprivate lazy var sign_image_container:ST_UserSignView = {
        let control = ST_UserSignView()
        return control
    }()
    
    
    fileprivate lazy var left_image_view:UIImageView = {
         let control = UIImageView()
          return control
      }()
    
    fileprivate lazy var left_number_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textAlignment = .center
        control.textColor = UIColor.init(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var avatar_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
            control.cornerRadius = 48/2.0
        control.borderColor = .clear
        control.borderWidth = 1
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(push_user_center_handle))
        control.isUserInteractionEnabled = true
        control.addGestureRecognizer(tap)
          return control
      }()

    fileprivate lazy var user_name_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
//    fileprivate lazy var sweet_value_label:UILabel = {
//        let control = UILabel.init()
//        return control
//    }()
    fileprivate lazy var sweet_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    var model:ST_GiftRankCellModel?{
        didSet{
            
            guard let m = model else{return}
            
            if let imageName = m.leftImageName{
                left_image_view.image = UIImage.init(named: imageName)
            }else{
                left_image_view.image = nil
            }
            
            left_number_label.text = (m.index+1).string
            
            
            avatar_image_view.pin_setImage(from: m.avatar.compress_small_image(width: 48*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            avatar_image_view.borderColor = m.borderColor
            
            user_name_label.text = m.nickname
            
            
            sweet_image_view.pin_setImage(from: m.intimacyImg.compress_small_image(width: 48*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            
            sign_image_container.bind_data(isVip: CHECK_MEMBER_STATUS(expirationTime: m.vipExpire) == .subscribe, detectedFace: m.faceAuditState == 3)
            
            
            
        }
    }
    
    
    @objc fileprivate func push_user_center_handle(){
        
        
        guard let m = model else { return  }
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = m.accid

        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
}




